var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');

Page(common.extendPage(MPage, {
    data: {
        dataLoaded: false,
        noteDetail: {},
        animationData: {},
        page: 1,
        loading: false,
        moreLoading: false,
        noMore: false,
        commentList: [],
        isShow: false,
        isFlag: false,
        replyData: {
            replyPage: 1,
            replyList: [],
            loading: false,
            noMore: false,
            moreLoading: false
        },
        commentTit: {},
        stickState: 0,
        focus: false,
        textareaValue: '',
        isLogin: false,
    },

    onLoad: function(options) {
        this.options = options;
        var _self = this;
        common.initApp({
            loginSuc: function() {
                _self.setData({
                    isLogin:!!app.getUserSid()
                });
                _self.$loginSuc();
                _self._getNoteDetail();
            }
        });
    },

    onShow(){
        this.setData({
            isLogin: !!app.getUserSid()
        });
        if (this.data.isLogin) {
            this._addPageView();
        }
    },
    onShareAppMessage: function() {
        return {
            title: app.getShop()['shareTitle'],
            path: "pages/cityInfo/cityInfo?noteId=" + this.options.noteId + "&stickState=" +this.options.stickState,
        }
    },
    onPullDownRefresh() {
        this._getNoteDetail();
    },
    onReachBottom: function() {
        this._loadMore();
    },
    /**
     * 获取信息详情
     */
    _getNoteDetail: function() {
        var _self = this;
        var url = "/openapi/CInformation/noteDetail";
        var paramsData = {
            noteId: this.options.noteId
        }
        ajax.G(url, paramsData, function(data) {

            var page_view = data.result.page_view;
            var zan_num = data.result.zan_num;
            if (page_view && page_view.length > 4) {
                data.result.page_view = parseInt(page_view / 10000) + '万+';
            }
            if (zan_num && zan_num.length > 4) {
                data.result.zan_num = parseInt(zan_num / 10000) + '万+';
            }

            _self.setData({
                noteDetail: data.result,
                dataLoaded: true,
                stickState: _self.options.stickState
            })
            _self._toTab({});
        })
    },
    /**
     * 调用打电话功能
     */
    _calling: function(evt) {
        var phoneNumber = evt.currentTarget.dataset.phoneNumber;
        wx.makePhoneCall({
            phoneNumber: phoneNumber, //此号码并非真实电话号码，仅用于测试  
            success: function() {},
            fail: function() {}
        })
    },
    /**
     * 预览图片
     */
    _previewImage: function(e) {
        var current = e.target.dataset.urlImage;
        var noteDetail = this.data.noteDetail;
        wx.previewImage({
            current: current,
            urls: noteDetail.imgUrls
        })
    },
    /**
     * 点赞
     */
    _clickZan: function() {
        var _self = this;
        var noteDetail = this.data.noteDetail;
        var zan_num = parseInt(noteDetail.zan_num);
        var url = '/openapi/CInformation/addZanNum';
        var paramsData = {
            noteId: this.options.noteId
        }
        ajax.P(url, paramsData, function(data) {
            _self.zanAnimation();
            zan_num += 1;
            noteDetail.haveZan = 1;
            noteDetail.zan_num = zan_num;
            _self.setData({
                noteDetail
            })
        })
    },
    /**
     * 浏览量
     */
    _addPageView: function() {
        var url = '/openapi/CInformation/addPageView';
        var paramsData = {
            noteId: this.options.noteId
        }
        ajax.P(url, paramsData, function(data) {})
    },
    _toTab: function(params, cb) {
        this.setData({
            page: 1,
            noMore: false,
        });
        this._getCommentList(params, cb, true);
    },
    /**
     * 获取信息列表
     */
    _getCommentList: function(params, cb, isReset) {
        var self = this;
        var isMore = params.isMore;
        var page = self.data.page || 1;
        var url = "/openapi/CInformation/getCommentList";
        var paramsData = {
            noteId: this.options.noteId,
            page
        }
        if (isMore) {
            self.setData({
                moreLoading: true
            });
        } else {
            self.setData({
                loading: true
            });
        }
        ajax.G(url, paramsData, function(data) {
            var list = data.result.commentList || [];
            var commentList = self.data.commentList;
            if (isReset) {
                commentList = [];
            }
            commentList.forEach(function(item) {
                item.replyNum = parseInt(item.replyNum);
            })
            var toData = {
                commentList: commentList.concat(list),
                noMore: list.length == 0,
                page: self.data.page + 1,
            };
            toData[isMore ? 'moreLoading' : 'loading'] = false;
            self.setData(toData);
            wx.stopPullDownRefresh() //停止下拉刷新
            cb && cb();
        });
    },
    /**
     * 加载更多
     */
    _loadMore: function() {
        if (this.data.moreLoading || this.data.loading || this.data.noMore) {
            return;
        }
        this._getCommentList({
            isMore: true
        });
    },
    /**
     * 打开回复列表
     */
    _openReplyList: function(evt) {
        var _self = this;
        var commentId = evt.currentTarget.dataset.commentId;
        this._commentId = commentId;
        var replyData = this.data.replyData;
        replyData.replyPage = 1;
        replyData.replyList = [];
        replyData.noMore = false;
        this.setData({
            replyData
        })
        var params = {
            replyData: this.data.replyData
        }
        this._getReplyList(params);
    },
    /**
     * 获取回复列表
     */
    _getReplyList: function(params, cb) {
        var _self = this;
        var isMore = params.isMore;
        var replyData = params.replyData;
        var replyPage = replyData.replyPage || 1;
        var replyList = replyData.replyList;
        var url = "/openapi/CInformation/getReplyList";
        var paramsData = {
            commentId: this._commentId,
            page: replyPage || 1
        }

        if (params.isMore) {
            replyData.moreLoading = true;
        } else {
            replyData.loading = true;
        }
        _self.setData({
            replyData
        });

        ajax.G(url, paramsData, function(data) {
            var list = data.result.replyList;
            var commentTit = data.result.comment;
            replyList = replyList.concat(list);
            replyData = {
                noMore: list.length == 0,
                replyPage: replyPage + 1,
                replyList
            }
            var toData = {
                replyData,
                commentTit
            };
            toData[params.isMore ? replyData.moreLoading : replyData.loading] = false;
            _self.setData(toData);
        })
    },
    /**
     * 获取更多回复
     */
    _getReplyMore: function() {
        var _self = this;
        if (this.data.replyData.moreLoading || this.data.replyData.loading || this.data.replyData.noMore) {
            return;
        }
        var params = {
            replyData: this.data.replyData,
            isMore: true
        }
        this._getReplyList(params);
    },
    /**
     * 打开输入框
     */
    _open: function(evt) {
        var {
            flag,
            noteId,
            commentId,
            replyUid
        } = evt.currentTarget.dataset;
        var _self = this;
        this._isFlag = flag;
        this._noteId = noteId;
        this._commentId = commentId;
        this._replyUid = replyUid;
        this.setData({
            isShow: true,
            isFlag: this._isFlag
        })
        setTimeout(function() {
            _self.setData({
                focus: true
            })
        }, 500)
    },
    /**
     * 关闭输入框
     */
    _close: function() {
        var _self = this;
        this.commentValue = '';
        this.setData({
            isShow: false,
            isFlag: false,
            textareaValue: ''
        })
        setTimeout(function() {
            _self.setData({
                focus: false
            })
        }, 500)
    },
    _getTextarea: function(evt) {
        this.commentValue = evt.detail.value;
    },
    /**
     * 提交信息
     */
    formSubmit: function(evt) {
        var _self = this;
        var isFlag = this._isFlag;
        var commentValue = this.commentValue;
        var url = "/openapi/CInformation/addComment";
        if (isFlag) {
            var noteId = this._noteId;
            var replyUid = this._replyUid;
            var commentId = this._commentId;
        }
        var paramsData = {
            noteId: noteId || this.options.noteId,
            commentId: commentId || 0,
            content: commentValue,
            replyUid: replyUid || ''
        }
        if (!commentValue) {
            util.alert('请输入您要评论的内容');
            return false;
        }
        ajax.P(url, paramsData, function(data) {
            console.log(app.getCheckComment());
            if (app.getCheckComment() == 1) {
                util.alert('您提交的评论或回复内容正在审核中,通过后会及时显示。')
            } else {
                if (isFlag) {
                    var replyData = _self.data.replyData;
                    var commentList = _self.data.commentList;
                    replyData.replyPage = 1;
                    replyData.noMore = false;
                    replyData.replyList = [];
                    var params = {
                        replyData
                    }
                    commentList.forEach(function(item) {
                        if (item.id == commentId) {
                            item.replyNum += 1;
                        }
                    })
                    _self.setData({
                        commentList
                    })
                    _self._getReplyList(params);

                } else {
                    _self._toTab({});
                }
            }

            _self._close();
        }, function() {
            _self._close();
        })
    },
    formReset: function() {},
    /**
     * 点赞动画
     */
    zanAnimation: function() {
        var animation = wx.createAnimation({
            duration: 1000,
            timingFunction: 'ease',
        })

        this.animation = animation;

        setTimeout(function() {
            animation.opacity(1).translate(15, -10).step();
            animation.opacity(0).step();
            this.setData({
                animationData: animation.export()
            })
        }.bind(this), 100);
    },
}))